
from flask import Flask, request, jsonify
from flask_cors import CORS  # 导入Flask-CORS库
import random
from http import HTTPStatus
# 建议dashscope SDK 的版本 >= 1.14.0
from dashscope import Generation
app = Flask(__name__)
# CORS(app, resources={r"/*": {"origins": "https://sd.ldjyypt.com"}}) # 只允许特定的来源
CORS(app) 
def call_with_messages():
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': request.get_json().get('message')}] #获取新的参数信息
    response = Generation.call(model="qwen-turbo",
                               api_key="sk-3c0aee84e5b74dfcb5e36b4b1679a97e",  # 替换为你的API密钥
                               messages=messages,
                               result_format='message')
    if response.status_code == HTTPStatus.OK:
        return response  # 返回解析后的JSON响应
    else:
        # 在出现错误时，返回包含错误信息的字典
        return {
            'error': {
                'request_id': response.request_id,
                'status_code': response.status_code,
                'code': response.code,
                'message': response.message
            }
        }


@app.route('/taskindex/newsindex', methods=['POST'])
def newsindex():
    # 调用call_with_messages函数并获取响应
    dashscope_response = call_with_messages()
    # 将Dashscope的响应封装到Flask的JSON响应中
    return jsonify(dashscope_response)
if __name__ == '__main__':
    app.run(debug=True)